package com.atguigu.ssyx.activity.controller;


import com.atguigu.ssyx.activity.service.ActivityInfoService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.activity.ActivityInfo;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.vo.activity.ActivityRuleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-04-07
 */
@RestController
@RequestMapping("/admin/activity/activityInfo")
//@CrossOrigin
@Api(tags = "活动管理")
public class ActivityInfoController {

    @Autowired
    private ActivityInfoService activityInfoService;

    /* url: `${api_name}/${page}/${limit}`,
     method: 'get'*/
    @ApiOperation("分页查询活动列表")
    @GetMapping("{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit) {
        LambdaQueryWrapper<ActivityInfo> wrapper = new LambdaQueryWrapper<>();

        //1.参加分页对象
        Page<ActivityInfo> p = new Page<>(page, limit);
        //2.执行查询
        activityInfoService.page(p);
        List<ActivityInfo> activityInfoList = p.getRecords();
        for (ActivityInfo activityInfo : activityInfoList) {
            //3.设置活动类型描述
            //数据库响应给前端的是FULL_REDUCTION,FULL_DISCOUNT，所以给activityTypeString字段添加描述
            activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        }
        return Result.ok(p);
    }


    /*url: `${api_name}/save`,
    method: 'post',
    data: role*/

    @ApiOperation("添加活动")
    @PostMapping("/save")
    public Result save(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.save(activityInfo);
        return Result.ok(null);
    }

    /* url: `${api_name}/get/${id}`,
     method: 'get'*/
    @ApiOperation("查询活动详情")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        ActivityInfo activityInfo = activityInfoService.getById(id);
        activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        return Result.ok(activityInfo);
    }

    /*url: `${api_name}/update`,
    method: 'put',
    data: role*/
    @ApiOperation("修改活动")
    @PutMapping("/update")
    public Result update(@RequestBody ActivityInfo activityInfo) {

        activityInfoService.updateById(activityInfo);
        return Result.ok(null);
    }

    /*url: `${api_name}/remove/${id}`,
    method: 'delete'*/
    @ApiOperation("删除活动")
    @DeleteMapping("/remove/{id}")
    public Result delete(@PathVariable Long id) {
        activityInfoService.removeById(id);
        return Result.ok(null);
    }

    /*url: `${api_name}/findActivityRuleList/${id}`,
    method: 'get'*/
    @ApiOperation("根据Id查询活动规则列表")
    @GetMapping("/findActivityRuleList/{id}")
    public Result findActivityRuleList(@PathVariable Long id) {
        //根据id查询活动规则列表和活动sku列表
        Map<String, Object> map = activityInfoService.findActivityRuleList(id);
        return Result.ok(map);
    }



    /*url: `${api_name}/saveActivityRule`,
    method: 'post',
    data: rule
*/

    @ApiOperation("在活动里面添加规则数据")
    @PostMapping("/saveActivityRule")
    public Result saveActivityRule(@RequestBody ActivityRuleVo activityRuleVo) {
        activityInfoService.saveActivityRule(activityRuleVo);
        return Result.ok(null);
    }
/*
    url: `${api_name}/findSkuInfoByKeyword/${keyword}`,
    method: 'get'*/

    @ApiOperation("根据关键字查询匹配sku信息")
    @GetMapping("/findSkuInfoByKeyword/{keyword}")
    public Result findSkuInfoByKeyword(@PathVariable String keyword) {
        List<SkuInfo> skuInfoList = activityInfoService.findSkuInfoByKeyword(keyword);
        return Result.ok(skuInfoList);
    }

}

